package com.prj.ufdm.demo.tutorial.zkcurator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
/**
 * 通過Curator客戶端調用API
 * @author Administrator
 *
 */
public class ClientApi {
	
	public static void main(String[] args) throws Exception {
		
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		CuratorFramework client = CuratorFrameworkFactory.newClient("47.98.183.17:2181", retryPolicy);
		client.start();
		
		String ZK_PATH = "/zkcurator/zktest";
		
		//  Create node
		String createData = "this is create data";
        client.create().creatingParentsIfNeeded().forPath(ZK_PATH, createData.getBytes());
        System.out.println("create :" + ZK_PATH + " data:"+createData);
        
        
        //  Get node and data
        System.out.println("ls / list:"+client.getChildren().forPath("/"));
        byte [] datas = client.getData().forPath(ZK_PATH);
        System.out.println("getData:"+new String(datas));
        
        //  Modify data
        String updateData = "this is update data";
        client.setData().forPath(ZK_PATH, updateData.getBytes());
        System.out.println("setData:"+updateData);
        
        // Remove data
        System.out.println("delete before ls /zkcurator list:"+client.getChildren().forPath("/zkcurator"));
        client.delete().forPath(ZK_PATH);
        System.out.println("delete after ls /zkcurator list:"+client.getChildren().forPath("/zkcurator"));
        
	}
	
}
